package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import de.hdodenhof.circleimageview.CircleImageView;
import liumengbaio.lolzhangyoubao.Activity.Adapter.ComentItemAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.ComentInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.ComentInfoUp;
import liumengbaio.lolzhangyoubao.R;

public class ItemRealShowActivity extends AppCompatActivity {
    private String item_up_url, item_down_url;
    private CircleImageView head_icon;
    private ImageView iv_icon_item;
    private TextView tv_nicker_name, tv_lol_name, tv_time, tv_good, self_say, tv_coment_item;
    private ComentInfo itemInfo;
    private ComentInfoUp itemdown;
    private ListView conment_listView;
    private ComentItemAdapter comentItemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_real_show);
        item_up_url = getIntent().getExtras().getString("item_up_url");
        item_down_url = getIntent().getExtras().getString("item_down_url");
        initView();
    }

    private void initView() {
        head_icon = (CircleImageView) findViewById(R.id.head_icon);
        iv_icon_item = (ImageView) findViewById(R.id.iv_icon_item);
        tv_nicker_name = (TextView) findViewById(R.id.tv_nicker_name);
        tv_lol_name = (TextView) findViewById(R.id.tv_lol_name);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_good = (TextView) findViewById(R.id.tv_good);
        self_say = (TextView) findViewById(R.id.self_say);
        conment_listView = (ListView) findViewById(R.id.conment_listView);
        tv_coment_item = (TextView) findViewById(R.id.tv_coment_item);
        findViewById(R.id.edit_head_left_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getDataFromWeb();
    }

    public void getDataFromWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(item_up_url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(ItemRealShowActivity.this, "数据获取失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        itemInfo = gson.fromJson(result, ComentInfo.class);
                        handler.sendEmptyMessage(1);
                    }
                });
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client1 = new OkHttpClient();
                Request request1 = new Request.Builder()
                        .url(item_down_url)
                        .build();
                Call call = client1.newCall(request1);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(ItemRealShowActivity.this, "数据获取错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result1 = response.body().string();
                        Gson gson = new Gson();
                        itemdown = gson.fromJson(result1, ComentInfoUp.class);
                        if (itemdown.getData().size() > 0) {
                            handler_coment.sendEmptyMessage(1);
                        } else {
                            handler_coment.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Picasso.with(ItemRealShowActivity.this).load(itemInfo.getData().getAvatar()).into(head_icon);
                Picasso.with(ItemRealShowActivity.this).load(itemInfo.getData().getPic_url()).into(iv_icon_item);
                tv_nicker_name.setText(itemInfo.getData().getNickname());
                tv_lol_name.setText(itemInfo.getData().getSummoner());
                tv_time.setText(itemInfo.getData().getCreated());
                tv_good.setText(itemInfo.getData().getGood());
                self_say.setText(itemInfo.getData().getDesc());
                tv_coment_item.setText(itemInfo.getData().getComment());
            }
        }
    };
    private Handler handler_coment = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                int totalHeight = 0;
                comentItemAdapter = new ComentItemAdapter(ItemRealShowActivity.this, itemdown);
                for (int i = 0; i < itemdown.getData().size(); i++) {
                    View view = comentItemAdapter.getView(i, null, conment_listView);
                    view.measure(0,0);
                    totalHeight += view.getMeasuredHeight();
                }
                ViewGroup.LayoutParams params=conment_listView.getLayoutParams();
                params.height=totalHeight+(conment_listView.getDividerHeight())*(comentItemAdapter.getCount()-1);
                conment_listView.setLayoutParams(params);
                conment_listView.setAdapter(comentItemAdapter);
            } else {
                Toast.makeText(ItemRealShowActivity.this, "数据出错", Toast.LENGTH_SHORT).show();
            }
        }
    };
}
